package com.example.administrator.historytoday;

import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private SimpleAdapter simpleAdapter;
    private ArrayList<Map<String,Object>> data;
    private Map<String,Object> map;

    private InputStreamReader inputStreamReader;
    private BufferedReader bufferedReader;
    private StringBuilder stringBuilder;

    private JSONObject article_list;

    /*
      去掉 标题
      1.android:theme="@android:style/Theme.Light.NoTitleBar"
      2.getSupportActionBar().hide();
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉标题
        setContentView(R.layout.activity_main);


        //获取xml控件id
        listView = findViewById(R.id.listView);

        //初始化数据源
        data = new ArrayList<Map<String,Object>>();

        try {
            //读取文件，字节流转 字符串
            inputStreamReader = new InputStreamReader(getAssets().open("data.json"),"utf-8");
            //字符缓冲，提升速度
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            //定义可变字符变量
            stringBuilder = new StringBuilder();
            while ( (line = bufferedReader.readLine()) != null){
                stringBuilder.append(line);
            }
            bufferedReader.close();
            inputStreamReader.close();

            try {
                //字符串转换json对象
                JSONObject jsonObject = new JSONObject(stringBuilder.toString());
                ////从JSONObject中取出数组对象
                JSONArray jsonArray = jsonObject.getJSONArray("list");

                //获取图片
                Resources res = getResources();
                for (int i=0;i<jsonArray.length();i++){
                    article_list = jsonArray.getJSONObject(i);//取出数组中的对象
                    map = new HashMap<String, Object>();

                    //根据名称获取资源id
                    int reid = getResources().getIdentifier(article_list.getString("img"),"drawable",getPackageName());
                    map.put("img",reid);
                    map.put("title",article_list.getString("title"));
                    data.add(map);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }


        } catch (IOException e) {
            e.printStackTrace();
        }


        String[] from = {"img","title"};
        int[] to = {R.id.img,R.id.title};
//        String[] from = {"title"};
//        int[] to = {R.id.title};

        simpleAdapter = new SimpleAdapter(MainActivity.this,data,R.layout.simpleadapter_item,from,to);
        //赋值
        listView.setAdapter(simpleAdapter);

        //监听单击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //1.获取listview 里的值
                //TextView  textView = view.findViewById(R.id._id);
                //String _id = textView.getText().toString();
                //Toast.makeText(MainActivity.this,"heheh"+_id,Toast.LENGTH_LONG).show();


                //2.获取listview 里的值
                //Map item = (Map)parent.getItemAtPosition(position);
                //Toast.makeText(MainActivity.this,"heheh"+item.get("post_content"),Toast.LENGTH_LONG).show();

                Map item = (Map)parent.getItemAtPosition(position);
                Intent intent = new Intent(MainActivity.this,ArticleActivity.class);
                intent.putExtra("title",item.get("title").toString());
                startActivity(intent);
            }
        });

    }
}
